package org.hawk.gwt.ppc;

import java.util.Arrays;

import junit.framework.TestCase;

public class CmdArgsTestCase extends TestCase {
	public void testBase(){
		CmdArgs args = createArgs("-Preflection","-target","aaa");
		assertEquals(args.getCompillerArguments(), "-target","aaa") ;
		assertEquals("aaa", args.get("-target"));
		assertTrue(args.get("reflection",false));
		assertNull(args.get("-Preflection"));
		assertNull(args.get("aaa"));
	}
	
	public void testProccesorArgs(){
		CmdArgs args = createArgs("-Preflection","-Pzzz=alfa");
		assertEquals(args.getCompillerArguments()) ;
		assertEquals("alfa", args.get("zzz"));
		assertTrue(args.get("reflection",false));
		assertNull(args.get("-Pzzz=alfa"));
		assertNull(args.get("alfa"));
		assertNull(args.get("-Pzzz"));
		assertNull(args.get("-Preflection"));
	}
	
	public void testNumericProccesorArgs(){
		CmdArgs args = createArgs("-Paaa=5");
		assertEquals(5, args.get("aaa",0).intValue());
		assertEquals("5",args.get("aaa"));
	}
	
	public void testModules(){
		CmdArgs args = createArgs("-logLevel","level",
			"-workDir","dir","-gen","dir","-style","style","-ea",
			"-XdisableClassMetadata","-XdisableCastChecking","-validateOnly",
			"-draftCompile","-compileReport","-localWorkers","count",
			"-war","dir","-extra","dir","module1","module2","module3");
		assertNotNull(args.getModules());
		assertEquals(args.getModules(),"module1","module2","module3");
	}
	
	public void testSingleArgBug(){
		CmdArgs args = createArgs("-draftCompile", "-war", 
				"/home/san/workspace", 
				"-localWorkers", "3", "-logLevel", "TRACE", 
				"-Preflect=org.hawk.admin.ui.**", "-Pkeep-preprocessed-sources", 
				"org.hawk.admin.ui.hawk_admin");
		assertEquals("org.hawk.admin.ui.**", args.get("reflect"));
		assertTrue(args.get("-draftCompile",false));
		assertEquals("/home/san/workspace", args.get("-war"));
	}

	private CmdArgs createArgs(String...args) {
		return new CmdArgs(args);
	}
	
	protected static void assertEquals(String[] args,String...expected){
		if(args==null||args.length!=expected.length){
			StringBuilder result = new StringBuilder();
			result.append("Expected ");
			result.append(Arrays.toString(expected));
			result.append(" but received ");
			result.append(Arrays.toString(args));
			throw new AssertionError(result.toString());
		}
		for(int i=0;i<args.length;i++){
			assertEquals(expected[i], args[i]);
		}
	}
}
